package com.ibl.rover.lite.handler;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.ibl.rover.lite.service.utill.Core;
import com.ibl.rover.lite.service.utill.Core.PRODUCTCATEGORY;
import com.ibl.rover.lite.service.utill.HibernateUtill;
import com.ibl.rover.lite.service.utill.Core.SST_TAGS;
import com.rover.lite.hibernate.dal.RvrCustomers;
import com.rover.lite.hibernate.dal.RvrPrdcategory;
import com.rover.lite.hibernate.dal.RvrProducts;
import com.rover.lite.hibernate.dal.RvrRouteqry;

public class ProductCategoryHandler {
	Session session;
	Document doc;

	public ProductCategoryHandler(Document _doc) {
		// TODO Auto-generated constructor stub
		session = HibernateUtill.getSessionFactory().openSession();
		doc = _doc;
    }
	
	
	
public void getProductCategory(Element bodyElement, String userName) {
	
		
		Criteria rvrPrdcategoryCriteria = session
				.createCriteria(RvrPrdcategory.class);
	
		rvrPrdcategoryCriteria.add(Restrictions.eq("id.tsoCode", userName));
		
		
		List<RvrPrdcategory> rvrPrdcategorylist = new ArrayList<RvrPrdcategory>();
		
		rvrPrdcategorylist =  rvrPrdcategoryCriteria.list();
		
		Element entityElement = doc.createElement("entity");
		entityElement.setAttribute("name", SST_TAGS.PRODUCT_CATEGORY);
		bodyElement.appendChild(entityElement);

		Element addElement = doc.createElement("add");
		entityElement.appendChild(addElement);
		
		for (RvrPrdcategory rvrPrdcategory : rvrPrdcategorylist) {
			
		List<RvrProducts> rvrProductsList = new ArrayList<RvrProducts>();
		
		Criteria rvrProductsCriteria=session.createCriteria(RvrProducts.class);
		rvrProductsCriteria.add(Restrictions.eq("id.tsoCode", userName));
		rvrProductsCriteria.add(Restrictions.eq("id.categcode", rvrPrdcategory.getId().getCategcode()));
		
		rvrProductsList = rvrProductsCriteria.list();
	
		if(rvrProductsList.size() < 1)
			continue;
		
		
	
			
				Element itemElement = doc.createElement("item");
				addElement.appendChild(itemElement);
				setRvrPrdcategoryAttribute(rvrPrdcategory, itemElement);
	
			
		}
	}

	
	private void setRvrPrdcategoryAttribute(RvrPrdcategory rvrPrdcategory,
			Element itemElement) {

		try {
			ArrayList<String> columnName = new ArrayList<String>();
			ArrayList<String> columnValue = new ArrayList<String>();
			//
			columnName.add(PRODUCTCATEGORY. CATEGCODE);
			columnValue.add(String.valueOf(rvrPrdcategory.getId().getCategcode()));

			Core.setAttribute(columnName, columnValue, itemElement, doc);
		} catch (Exception e) {

		}

	}


	
}
